//
//  PileUpViewController.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/8.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit

class PileUpViewController: UIViewController {

    let cellSize = CGSize(width: 300, height: 100)
    let colors: [UIColor] = [UIColor.init(named: "Grey")!, UIColor.init(named: "LightOrange")!, UIColor.init(named: "LightRed")!, UIColor.init(named: "Primary")!]

    lazy var collectionView: UICollectionView = {
        
        let bgImages = UIImageView.init(frame: self.view.frame)
        bgImages.image = UIImage.init(named: "5")
        
        let layout = PileUpLayout.init()
        layout.itemSize = cellSize
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
//        layout.scrollDirection = .horizontal
        
        let cv                    = UICollectionView.init(frame : self.view.frame, collectionViewLayout : layout)
        cv.delegate               = self
        cv.dataSource             = self
        cv.backgroundView         = bgImages
        cv.register(UINib.init(nibName       : "ScrollCell", bundle       : nil), forCellWithReuseIdentifier       : "ScrollCellID2")
        return cv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
    }
    
}

extension PileUpViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ScrollCellID2", for: indexPath) as! ScrollCell
//        cell.imgView.backgroundColor = colors[indexPath.row%4]
        cell.imgView.backgroundColor = .white
        cell.imgView.layer.masksToBounds = true
        cell.imgView.layer.borderColor = UIColor.gray.cgColor
        cell.imgView.layer.borderWidth = 1
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldSpringLoadItemAt indexPath: IndexPath, with context: UISpringLoadedInteractionContext) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        return proposedContentOffset
    }
    
    func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
        return proposedIndexPath
    }
}
